package com.youi.web;

import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.entity.User;
import com.youi.entity.Useraddress;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.service.UseraddressService;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Useraddress控制层
* @author Administrator
*/
@Controller
public class UseraddressController  extends HttpRequestManage{

		@Autowired
		private UseraddressService useraddressService;

		/**
		 * M-4
		 * 添加或编辑用户地址
		*/
		@ResponseBody
		@RequestMapping("userGetInfo/addOrEditUseraddress")
		public String addOrEditUseraddress(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Long id=optLong("id",request);
			Useraddress useraddress=null;
			if (id!=null) {
				useraddress=useraddressService.get(id);
			}else {
				useraddress=new Useraddress();
				useraddress.setDelStatus(DelStatus.NODEL);
				useraddress.setGmtCreated(new Date());
				useraddress.setUserId(user.getId());
				useraddressService.saveOrUpdate(useraddress);
			}
			Integer defaults=optInteger("defaults",request);
			if (defaults!=null && defaults == 1){
				useraddress.setDefaults(defaults);
				useraddressService.setDefultsAddress(useraddress, user);
			}
			String contacts=optString("contacts",request);
			if (contacts!=null){
				useraddress.setContacts(contacts);
			}
			String province=optString("province",request);
			if (province!=null){
				useraddress.setProvince(province);
			}
			String city=optString("city",request);
			if (city!=null){
				useraddress.setCity(city);
			}
			String districts=optString("districts",request);
			if (districts!=null){
				useraddress.setDistricts(districts);
			}
			String phone=optString("phone",request);
			if (phone!=null){
				useraddress.setPhone(phone);
			}
			String address=optString("address",request);
			if (address!=null){
				useraddress.setAddress(address);
			}
			user.setGmtModify(new Date());
			useraddressService.saveOrUpdate(useraddress);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * M-5
		 * 删除用户地址
		*/
		@ResponseBody
		@RequestMapping("userGetInfo/delUseraddressByIds")
		public String delUseraddressByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Useraddress> useraddressList= useraddressService.getByParams(params);
			if (useraddressList==null||useraddressList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Useraddress useraddress : useraddressList) {
				useraddress.setDelStatus(DelStatus.DEL);
			}
			useraddressService.saveOrUpdateAll(useraddressList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * M-2
		 * 查询用户地址列表
		*/
		@ResponseBody
		@RequestMapping("userGetInfo/getUseraddress")
		public String getUseraddress(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put(Common.USERID, user.getId());
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Useraddress> useraddressList=useraddressService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (useraddressList==null||useraddressList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=useraddressService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, useraddressList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * M-3
		 * 查询用户地址详情
		*/
		@ResponseBody
		@RequestMapping("userGetInfo/getUseraddressById")
		public String getUseraddressById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Useraddress useraddress=useraddressService.get(id);
			if (useraddress==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",useraddress);//jsonObject	json对象
		}

		/**
		 * M-17
		 * 查询用户默认地址详情(若无设置默认地址则返回最新添加的地址)
		*/
		@ResponseBody
		@RequestMapping("userGetInfo/getDefultUseraddress")
		public String getDefultUseraddress(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put(Common.USERID, user.getId());
			prams.put("defaults", Common.TRUE);
			Useraddress useraddress=useraddressService.getByParam(prams);
			if (useraddress==null) {
				prams=new HashMap<String, Object>();
				prams.put(Common.DELSTATUS, DelStatus.NODEL);
				prams.put(Common.USERID, user.getId());
				Map<String, Object> sortPram=new HashMap<String, Object>();
				sortPram.put(Common.ID,Common.DESC);
				useraddress=useraddressService.getByParam(prams, null, sortPram);
				if (useraddress==null) {
					return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
				}
			}			
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",useraddress);//jsonObject	json对象
		}
		
		/**
		 * M-6
		 * 设置默认地址
		*/
		@ResponseBody
		@RequestMapping("userGetInfo/updateDefultAddress")
		public String updateDefultAddress(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Integer defaults=getInteger("defaults", request);
			User user=(User) HttpSessionManage.getAttribute(request, SessionKey.USER);			
			Useraddress useraddress=useraddressService.get(id);
			if (useraddress==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			useraddress.setDefaults(defaults);
			useraddressService.setDefultsAddress(useraddress, user);
			useraddressService.saveOrUpdate(useraddress);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");//jsonObject	json对象
		}
		
		/**
		 * 查询所有Useraddress
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllUseraddress")
		public String getAllUseraddress(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Useraddress> useraddressList= useraddressService.getByParams(null);
			if (useraddressList==null||useraddressList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",useraddressList);//jsonObjectList	json对象集合
		}
}
